/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.PaginationUtil;
import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.UserListingForm;
import com.ntb.entities.UserGroups;
import com.ntb.entities.Users;
import com.ntb.models.UserGroupsDAO;
import com.ntb.models.UsersDAO;
import com.ntb.models.impl.UserGroupsDAOImpl;
import com.ntb.models.impl.UsersDAOImpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class UserListingAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        UserListingForm userListingForm = (UserListingForm) form;

        UsersDAO usersDao = new UsersDAOImpl();

        String action = userListingForm.getAction();

        List<String> errorMessages = new ArrayList();
        List<String> successMessages = new ArrayList();
        boolean isHaveAction = true;
        if (("delete").equalsIgnoreCase(action)) {
            if (userListingForm.getUserID() == this.currentUser.getUserID()) {
                errorMessages.add("You cannot delete yourself!");
            } else {
                usersDao.delete(userListingForm.getUserID());
                successMessages.add("1 users out of 1 successfully deleted.");
            }
        } else if (("deleteUsers").equalsIgnoreCase(action)) {
            int[] userIds = userListingForm.getUserIDs();
            int count = 0;
            int length = 0;

            try {
                length = userIds.length;
            } catch (Exception ex) {
            }

            if (length > 0) {
                for (int userID : userIds) {
                    if (userID == this.currentUser.getUserID()) {
                        errorMessages.add("You cannot delete yourself!");
                    } else {
                        usersDao.delete(userID);
                        count++;
                    }
                }
                successMessages.add(count + " users out of " + length + " successfully deleted.");
            }
        } else if (("activeUsers").equalsIgnoreCase(action)) {
            int[] userIds = userListingForm.getUserIDs();
            int count = 0;

            int length = 0;

            try {
                length = userIds.length;
            } catch (Exception ex) {
            }

            if (length > 0) {
                for (int userID : userIds) {
                    if (usersDao.active(userID) > 0) {
                        count++;
                    }
                }
                successMessages.add(count + " users out of " + length + " successfully activated.");
            }
        } else {
            isHaveAction = false;
        }

        UserGroupsDAO userGroupsDao = new UserGroupsDAOImpl();
        Iterator<UserGroups> groupList = userGroupsDao.getList();
        request.setAttribute("groupList", groupList);

        String baseUrl = request.getRequestURI() + "?";

        String username = null;
        int groupID = -1;
        int active = -1;
        int page = 1;

        username = userListingForm.getUname();
        username = "".equals(username) ? null : username;
        if (username != null) {
            baseUrl += "uname=" + username + "&amp;";
        }

        groupID = userListingForm.getGroup();
        if (groupID > 0) {
            baseUrl += "group=" + groupID + "&amp;";
        }

        active = userListingForm.getAct();
        if (active == 1 || active == 2) {
            baseUrl += "act=" + active + "&amp;";
            active--;
        } else {
            active = -1;
        }

        try {
            int i = Integer.parseInt(request.getParameter("page"));
            page = i > 1 ? i : 1;
        } catch (Exception ex) {
        }

        Iterator<Users> userList = usersDao.getList(username, groupID, active, page);
        if (isHaveAction) {
            request.setAttribute("errorMessages", errorMessages);
            request.setAttribute("successMessages", successMessages);
        }

        request.setAttribute("userList", userList);

        PaginationUtil paginationUtil = new PaginationUtil();
        paginationUtil.setBaseUrl(baseUrl);
        paginationUtil.setNumPages(usersDao.getPageCount(username, groupID, active));
        paginationUtil.setCurPage(page);
        request.setAttribute("pagination", paginationUtil.createLinks());

        return mapping.getInputForward();
    }
}
